/*
 * Copyright 2012 Jonathan St-Michel <jonathan@abiwebz.net>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.abiwebz.office.rm.database;

import java.sql.Connection;
import java.util.prefs.Preferences;
import org.openide.util.NbPreferences;

/**
 * DataConnections provide access to Online and Offline connection.
 * This class manage Online and Offline connection and provide functions like
 * setter, getter and check.
 * 
 * @author Jonathan St-Michel <jonathan@abiwebz.net>
 */
public class DataConnections {
    
    private static DataConnections instance; // Singleton instance
    
    // Connections - these are initialized by SecurityManager
    private Connection onlineConnection;
    private Connection offlineConnection;
    
    /**
     * Create new instance of DataConnections.
     * 
     * @return New instance of DataConnections
     */
    public static DataConnections create(){
        DataConnections.instance = new DataConnections();
        return DataConnections.instance;
    }
    
    /**
     * Return default instance of DataConnections class. If no instance 
     * exist because DataConnections.create() was not called before, the function
     * will try to create an instance before returning it.
     * 
     * @return Default DataConnections instance
     */
    public static DataConnections get(){
        if(DataConnections.instance == null){ //No instance is created
            return DataConnections.create();
        }
        else{ //We already have an existing instance
            return DataConnections.instance;
        }
    }
    
    //==================== END OF STATIC FUNCTIONS =============================

    Preferences nbprefs; // Application configuration for DatabaseDrivers class
    
    /**
     * Constructor for DataConnections. Initialze the configuration by
     * loading the DatabaseDrivers class configuration.
     */
    public DataConnections() {
        nbprefs = NbPreferences.forModule(DatabaseDrivers.class);
    }
    
    /**
     * Get the Online connection. Used in case we want the connection to the
     * online database when the offline connection is the default one.
     * 
     * @return SQL Connection
     */
    public Connection getOnlineConnection(){
        return this.onlineConnection;
    }
    
    /**
     * Get the Offline connection. Used in case we want the connection to the
     * offline database when the online connection is the default one.
     * 
     * @return SQL Connection
     */
    public Connection getOfflineConnection(){
        return this.offlineConnection;
    }
    
    /**
     * Get the Default connection. Default connection is based on the 
     * application configuration. It is the best way to get the connection
     * without mistakes.
     * 
     * @return SQL Connection
     */
    public Connection getConnection(){
        String location = nbprefs.get("database.connection", "online"); // By default use the online connection
        if(location.equals("online")){
            return getOnlineConnection();
        }
        else{
            return getOfflineConnection();
        }
    }
    
    /**
     * Set the default connection. Default connection is based on the 
     * application configuration. It is the best way to get the connection
     * without mistakes.
     * 
     * @param connection SQL connection
     */
    public void setConnection(Connection connection){
        String location = nbprefs.get("database.connection", "online"); // By default use the online connection
        if(location.equals("online")){
            setOnlineConnection(connection);
        }
        else{
            setOfflineConnection(connection);
        }
    }
    
    /**
     * Set the Online connection. Used in case we want the connection to the
     * online database when the offline connection is the default one.
     * 
     * @param connection SQL connection
     */
    public void setOnlineConnection(Connection connection){
        this.onlineConnection = connection;
    }
    
    /**
     * Set the Offline connection. Used in case we want the connection to the
     * offline database when the online connection is the default one.
     * 
     * @param connection SQL connection
     */
    public void setOfflineConnection(Connection connection){
        this.offlineConnection = connection;
    }
    
    /**
     * Check if a configuration exist for offline database. This function
     * is used to block functionalities that need the offline database.
     * 
     * @return True if offline database is configured
     */
    public boolean isOfflineEnabled(){
        return nbprefs.getBoolean("database.driver.offline.set", false);
    }
    
    /**
     * Check if online database is loaded and connected.
     * 
     * @return True if online database is ready
     */
    public boolean isOnlineReady(){
        return this.onlineConnection != null;
    }
    
    /**
     * Check if offline database is loaded and connected.
     * 
     * @return True if offline database is ready
     */
    public boolean isOfflineReady(){
        return this.offlineConnection != null;
    }
    
}
